#include "Stats.h"

#include "rapidxml.hpp"
#include "rapidxml_utils.hpp"

#include <iostream>
#include <string>

using namespace rapidxml;

/* init members as 0 */
Stats::Stats()
{
	mHealth = 0.0;
	mEnergy = 0.0;
	mDamage = 0.0;
	mRange = 0.0;
	mR_exp = 0.0;
	mR_health = 0.0;
	mR_energy = 0.0;
	mR_skill = 0.0;
}

Stats::Stats(boost::shared_ptr<SettingsManager> sm)
{
	readStatsFromXML(sm);
}

Stats::~Stats(void)
{

}

SettingsManager* Stats::getSettings()
{
	return mpSM.get();
}

float Stats::getReward(RewardType type)
{
	float result = 0.0;
	switch(type)
	{
		case REW_EXP: result = mR_exp; break;
		case REW_HEALTH: result = mR_health; break;
		case REW_ENERGY: result = mR_energy; break;
		case REW_SKILL: result = mR_skill; break;
		default: break;
	}

	return result;
}

float Stats::getStat(StatType type)
{
	float result;
	switch(type)
	{
		case STAT_HEALTH: result = mHealth; break;
		case STAT_ENERGY: result = mEnergy; break;
		case STAT_DAMAGE: result = mDamage; break;
		case STAT_RANGE: result = mRange; break;
		default: result = 0.0;
	}

	return result;
}

void Stats::setStat(StatType type, float value)
{
	switch(type)
	{
		case STAT_HEALTH: mHealth = value; break;
		case STAT_ENERGY: mEnergy = value; break;
		case STAT_DAMAGE: mDamage = value; break;
		case STAT_RANGE: mRange = value; break;
		default: break;
	}
}



bool Stats::readStatsFromXML(boost::shared_ptr<SettingsManager> sm)
{
	mHealth = (float)atof(sm->getSetting("Resources/Health/cap").c_str());
	mEnergy = (float)atof(sm->getSetting("Resources/Energy/cap").c_str());
	mDamage = (float)atof(sm->getSetting("Combat/damage").c_str());
	mRange = (float)atof(sm->getSetting("Combat/range").c_str());

	//load reward settings
	mR_exp = (float)atof(sm->getSetting("Reward/exp").c_str());
	mR_health = (float)atof(sm->getSetting("Reward/health").c_str());
	mR_energy = (float)atof(sm->getSetting("Reward/energy").c_str());
	mR_skill = (float)atof(sm->getSetting("Reward/skill").c_str());

	//update SettingsManager in case it has been loaded from a different one.
	mpSM = sm;

	return true;
}